<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

/**
 * 活动主办方管理
 */
class manage extends admin{
	private $db;
	public $siteid;
	public $admin_userid;
	
	public function __construct(){
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('organizer_model');
		$this->siteid = get_siteid();
		$this->admin_userid = param::get_cookie('userid');//获取当前登录的管理员ID
		pc_base::load_sys_class('form','',0);
		pc_base::load_sys_class('format','',0);
	}
	
	public function index() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where .= "siteid = ".$this->siteid." and status = 1";
		if (isset($_GET['dosubmit'])) {
			$keyword = $_GET['organizer']['name'];
			$where .= " and name like '%$keyword%'";
		}
		$result = $this->db->listinfo($where,'id DESC',$page,10);
		$admin_model = pc_base::load_model('admin_model');//加载管理员model
		$adminInfo = $admin_model->getRealnameInfo();
		foreach ($result as $k=>$v){
			//获取用户管理员真实名称数据
			$result[$k]['username'] = $adminInfo[$v['updated_by']];
		}
		$pages = $this->db->pages;
		include $this->admin_tpl('organizer_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$info = $_POST['organizer'];
			if (empty(trim($info['email'])) && empty(trim($info['phone'])) && empty(trim($info['qq']))) {
				showmessage(L('phone_email_qq'),HTTP_REFERER);
			}
			$data['name'] = $info['name'];
			$data['description'] = $info['desc'];
			$data['logo'] = $info['logo'];
			$data['link'] = $info['link'];
			$data['email'] = $info['email'];
			$data['telephone'] = $info['phone'];
			$data['qq'] = $info['qq'];
			$data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
			$data['created_by'] = $data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$result = $this->db->insert($data,true);
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:green;">添加主办方</span>[主办方id：'.$result.'，主办方名称：'.$info['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('add_success'),'?m=activity&c=manage&a=index');
			} else {
				showmessage(L('add_error'),HTTP_REFERER);
			}
		} else {
			include $this->admin_tpl('organizer_add');
		}
	}
	
	public function edit() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		if (isset($_POST['dosubmit'])) {
			$info = $_POST['organizer'];
			if (empty(trim($info['email'])) && empty(trim($info['phone'])) && empty(trim($info['qq']))) {
				showmessage(L('phone_email_qq'),HTTP_REFERER);
			}
			$data['name'] = $info['name'];
			$data['description'] = $info['desc'];
			$data['logo'] = $info['logo'];
			$data['link'] = $info['link'];
			$data['email'] = $info['email'];
			$data['telephone'] = $info['phone'];
			$data['qq'] = $info['qq'];
			$data['update_time'] = date('Y-m-d H:i:s',time());
			$data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				//记录行为日志
				$log_content = '<span style="color:blue;">修改主办方</span>[主办方id：'.$id.'，主办方名称：'.$info['name'].']';
				$this->manage_admin_log($log_content);
				showmessage(L('edit_success'),'?m=activity&c=manage&a=index');
			} else {
				showmessage(L('add_error'),HTTP_REFERER);
			}
		} else {
			$info = $this->db->get_one(array('id'=>$id));
			$show_header = false;
			include $this->admin_tpl('organizer_edit');
		}
	}
	
	public function del() {
		if (!isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
			showmessage(L('message_not_exits'),HTTP_REFERER);
		}
		$id = $_REQUEST['id'];
		if (is_array($id)) {
			foreach ($id as $k=>$v){
				if (intval($v)) {
					$id[$k] = $v;
				} else {
					unset($id[$k]);
				}
			}
			$id = implode('\',\'',$id);
		}
		$where = "id in ('$id')";
		$organizer_name = $this->db->select($where,'name','','id DESC');
		foreach ($organizer_name as $v){
			$name[] = $v['name'];
		}
		$result = $this->db->update(array('status'=>'2'),"id in ('$id')");
		if ($result) {
			$del_id = explode('\',\'', $id);
			$new_id = implode('，', $del_id);
			//记录行为日志
			$log_content = '<span style="color:red;">删除主办方</span>[主办方id：'.$new_id.',主办方名称：'.json_encode($name,JSON_UNESCAPED_UNICODE).']';
			$this->manage_admin_log($log_content);
			showmessage(L('del_success'),'?m=activity&c=manage&a=index');
		} else {
			showmessage(L('del_error'),HTTP_REFERER);
		}
	}
	
	public function detail() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message_not_exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		$show_header = false;
		include $this->admin_tpl('organizer_detail');
	}
}